package j_composite.a_design_pattern;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wug on 2016/7/14 0014.
 * 组合
 */
public class Composite extends Component {
    List<Component> componentList = new ArrayList<>();

    public Composite(String name) {
        super.name = name;
    }

    @Override
    public void operation() {
        /**
         * 做一些辅助操作
         */
        System.out.println(name);
        /**
         * 将后面的交给子节点做
         */
        for (Component component : componentList) {
            System.out.print("-");
            component.operation();
        }
    }

    @Override
    public Component add(Component component) {
        componentList.add(component);
        return this;
    }

    @Override
    protected Component remove(Component component) {
        componentList.remove(component);
        return this;
    }

    @Override
    protected Component getChild(int i) {
        return componentList.get(i);
    }
}
